/** 登录与注册验证 */
import type { Rule } from 'ant-design-vue/es/form'
import { t } from '@/i18n'

const REGEXP_PWD =
  /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[()])+$)(?!^.*[\u4E00-\u9FA5].*$)([^(0-9a-zA-Z)]|[()]|[a-z]|[A-Z]|[0-9]){8,18}$/

/** 登录表单规则 */
export const loginRules: Record<string, Rule[]> = {
  phone: [
    {
      required: true,
      message: t('account.validation.phoneRequired'),
      trigger: 'blur',
    },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: t('account.validation.phoneInvalid'),
      trigger: 'blur',
    },
  ],
  passport: [
    {
      required: true,
      message: t('account.validation.usernameRequired'),
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      message: t('account.validation.passwordRequired'),
      trigger: 'blur',
    },
  ],
  code: [
    {
      required: true,
      message: t('account.validation.codeRequired'),
      trigger: 'blur',
    },
  ],
}

/** 注册表单规则 */
export const registRules: Record<string, Rule[]> = {
  phone: [
    {
      required: true,
      message: t('account.validation.phoneRequired'),
      trigger: 'blur',
    },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: t('account.validation.phoneInvalid'),
      trigger: 'blur',
    },
  ],
  email: [
    {
      type: 'email',
      message: t('account.validation.emailInvalid'),
      trigger: 'blur',
    },
  ],
  password: [
    {
      validator: async (rule, value) => {
        if (value === '') {
          return Promise.reject(t('account.validation.passwordRequired'))
        } else if (!REGEXP_PWD.test(value)) {
          return Promise.reject(t('account.validation.passwordInvalid'))
        } else {
          return Promise.resolve()
        }
      },
      trigger: 'blur',
    },
  ],
  code: [
    {
      required: true,
      message: t('account.validation.codeRequired'),
      trigger: 'blur',
    },
  ],
}
